package com.example.itshome.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.bumptech.glide.Glide;
import com.example.itshome.R;
import com.example.itshome.activity.ActivityListPageActivity;
import com.example.itshome.activity.ShopPageActivity;
import com.example.itshome.adapter.MainPageFragmentAdapter;
import com.example.itshome.manager.UserInfoManager;
import com.example.itshome.onclick.OnClickTurnTo;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class MainPageFragment extends Fragment implements UserInfoManager.OnUpdateUserRunner {

    private ViewPager banner;
    private ImageView headerImageView;
    private View activityLayout;
    private View shopLayout;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_main_page, container, false);
        initFind(view);
        initButton();
        initBanner(view);
        UserInfoManager.getInstance().addRunners(this);
        return view;
    }


    void initFind(View v){
        activityLayout=v.findViewById(R.id.layout_main_page_activity);
        shopLayout=v.findViewById(R.id.layout_main_page_shop);
        banner=v.findViewById(R.id.vp_main_page);
        headerImageView=v.findViewById(R.id.imv_main_page_header);
    }

    void initButton(){
        activityLayout.setOnClickListener(new OnClickTurnTo(getActivity(), ActivityListPageActivity.class));
        shopLayout.setOnClickListener(new OnClickTurnTo(getActivity(), ShopPageActivity.class));
    }

    void initView(){
        if(getContext()!=null)
        Glide.with(this).load(UserInfoManager.getInstance().getUser().getHeaderSrc()).circleCrop().placeholder(R.drawable.sample_picture).into(headerImageView);
    }

    private void initBanner(View v){
        List<Fragment> fragments=new ArrayList<>();
        Fragment f1=new MainPageBannerItemFragment();
        Bundle b1 = new Bundle();
        b1.putInt("bgResourceId", R.drawable.banner_item1_background);
        b1.putStringArray("description",new String[]{"刺猬","\"别看我小小一只，里面全是腿\""});
        f1.setArguments(b1);
        Fragment f2=new MainPageBannerItemFragment();
        Bundle b2 = new Bundle();
        b2.putInt("bgResourceId", R.drawable.banner_item2_background);
        b2.putStringArray("description",new String[]{"三花花 北洋园","\"最近本喵正在减肥中，罐头和猫条打咩\""});
        f2.setArguments(b2);
        Fragment f3=new MainPageBannerItemFragment();
        Bundle b3 = new Bundle();
        b3.putStringArray("description",new String[]{"鸭鸭","\"今天去哪干饭?\"\"我跟着你吧我也不造\""});
        b3.putInt("bgResourceId", R.drawable.banner_item3_background);
        f3.setArguments(b3);

        fragments.add(f1);
        fragments.add(f2);
        fragments.add(f3);
        banner.setAdapter(new MainPageFragmentAdapter(getChildFragmentManager(),fragments));
        banner.setCurrentItem(1);
        banner.setPageTransformer(true, new ViewPager.PageTransformer() {
            @Override
            public void transformPage(@NonNull View page, float position) {
                 if (position >= -1&&position <= 1){
                    float scaleFactor = Math.max(0.5f, 1 - 0.2f*Math.abs(position));

                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);

                }

            }
        });
    }

    @Override
    public void onInfoUpdate() {
        initView();
    }
}